package com.hmall.controller.back;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.BiUserActionHistoryMapper;
import com.hmall.mapper.ComSendMsgMapper;

import com.hmall.model.ComSendMsg;

import com.hmall.vo.SendMsgManageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.*;


@RestController
@RequestMapping("/manage/send")
@Api(value = "后台消息推送管理操作API", description = "后台消息推送管理类API说明")
public class SendMsgManageController {
    private final
    ComSendMsgMapper comSendMsgMapper;

    @Autowired
    public SendMsgManageController(ComSendMsgMapper comSendMsgMapper, BiUserActionHistoryMapper biUserActionHistoryMapper) {
        this.comSendMsgMapper = comSendMsgMapper;
    }

    @PostMapping("CreatedSendMsg")
    @ApiOperation(value = "添加消息推送")
    public ServerResponse<String> add(@RequestBody
                                              ComSendMsg comSendMsg) {
        Timer timer = new Timer();
        timer.schedule(new RemindTask(comSendMsg), comSendMsg.getSendTime());

        Date date = new Date();
        comSendMsg.setCreateTime(date);
        comSendMsg.setIsSend(0);
        comSendMsgMapper.insert(comSendMsg);

        //timer.cancel();
        return ServerResponse.createBySuccess("创建成功");
    }

    @GetMapping("GetSendMsgList")
    @ApiOperation(value = "获取消息推送列表")
    public ServerResponse<PageInfo<ComSendMsg>> GetSendMsgList(
            @RequestParam(value = "pageNum", defaultValue = "1")
                    int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "100")
                    int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<ComSendMsg> freightList = comSendMsgMapper.selectAllSendMsg();
        PageInfo<ComSendMsg> pageResult = new PageInfo<>(freightList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @DeleteMapping("DeleteSendMsg")
    @ApiOperation(value = "删除消息推送")
    public ServerResponse DeleteSendMsg(Integer[] id) {
        int push_msg_id;
        int count = 0;

        for (Integer anId : id) {
            push_msg_id = anId;
            count = count + comSendMsgMapper.deleteByPrimaryKey(push_msg_id);
        }

        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        } else {
            return ServerResponse.createBySuccess("删除成功");
        }
    }

    @PostMapping("EditSendMsg")
    @ApiOperation(value = "修改消息推送")
    public ServerResponse EditSendMsg(
            @RequestBody
                    SendMsgManageVo sendMsgManageVo) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sendMsgManageVo.toString());

        ComSendMsg sendMsgManage = new ComSendMsg();
        sendMsgManage.setIsSend(sendMsgManageVo.getIsSend());

        Date create_time = df.parse(sendMsgManageVo.getCreateTime());
        sendMsgManage.setCreateTime(create_time);

        Date send_time = df.parse(sendMsgManageVo.getSendTime());
        sendMsgManage.setSendTime(send_time);
        sendMsgManage.setAudiencesChannel(sendMsgManageVo.getAudiencesChannel());
        sendMsgManage.setBody(sendMsgManageVo.getBody());
        sendMsgManage.setTitle(sendMsgManageVo.getTitle());
        sendMsgManage.setPushMsgId(sendMsgManageVo.getPushMsgId());
        sendMsgManage.setValue(sendMsgManageVo.getValue());
        sendMsgManage.setUsername(sendMsgManageVo.getUsername());
        sendMsgManage.setSendType(sendMsgManageVo.getSendType());
        comSendMsgMapper.updateByPrimaryKey(sendMsgManage);

        //        int count = sendMsgManageMapper.updateByPrimaryKey(sendMsgManage);
        //        if(count==0){
        //            return ServerResponse.createByErrorMessage("修改失败");
        //        }else{
        //            return ServerResponse.createBySuccess("修改成功");
        //        }
        return null;
    }

    @GetMapping("GetSendMsgs")
    @ApiOperation(value = "获得消息推送信息")
    public ServerResponse<ComSendMsg> GetSendMsgs(Integer id) {
        ComSendMsg sendMsgManage = comSendMsgMapper.selectByPrimaryKey(id);

        return ServerResponse.createBySuccess(sendMsgManage);
    }

    @GetMapping("SearchSendMsg")
    @ApiOperation(value = "搜索消息推送")
    public ServerResponse<PageInfo<ComSendMsg>> SearchSendMsg(String query,
                                                              @RequestParam(value = "pageNum", defaultValue = "1")
                                                                      int pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "100")
                                                                      int pageSize) {
        System.out.println(query);
        PageHelper.startPage(pageNum, pageSize);

        List<ComSendMsg> freightList = comSendMsgMapper.selectListByTitle(query);
        PageInfo<ComSendMsg> pageResult = new PageInfo<>(freightList);

        return ServerResponse.createBySuccess(pageResult);
    }

    //
    //    @GetMapping("searchUserByArea")
    //    @ApiOperation(value = "通过地区搜索用户")
    //    public ServerResponse searchUserByArea(String province,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
    //                                        @RequestParam(value = "pageSize",defaultValue = "100") int pageSize){
    //        System.out.println(province);
    //        PageHelper.startPage(pageNum,pageSize);
    //        List<UserAreaVo> userAreaVoList = biUserActionHistoryMapper.selectListByArea(province);
    //        PageInfo pageResult = new PageInfo(userAreaVoList);
    //        return ServerResponse.createBySuccess(pageResult);
    //    }
}
